package com.taskcollection.control;

import com.alibaba.fastjson.JSONObject;
import com.taskcollection.bean.ProjectTeamBean;
import com.taskcollection.bean.StudentBean;
import com.taskcollection.bean.StudentProjectTeamBean;
import com.taskcollection.service.ProjectTeamService;
import com.taskcollection.service.StudentProjectTeamService;
import com.taskcollection.service.StudentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by haocongshun on 2016/8/18.
 */
public class StudentProjectTeamControl {
    public void studentProjectTeam(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentBean student = (StudentBean) request.getSession().getAttribute("学生");
        StudentProjectTeamService spts = new StudentProjectTeamService();
        String flag = request.getParameter("flag");
        JSONObject json = new JSONObject();
        if(student==null){
            json.put("msg",false);
        }else{
            if("join".equals(flag)){
                if(spts.getByStudentId(student.getStudent_id())!=null){
                    StudentProjectTeamBean sptb =spts.getByStudentId(student.getStudent_id());
                    StudentService ss = new StudentService();
                    sptb.setStudent(ss.getById(sptb.getStudent_id()));
                    ProjectTeamService pts = new ProjectTeamService();
                    sptb.setProjectTeam(pts.getById(sptb.getProjectTeam_id()));
                    json.put("msg",sptb);
                    System.out.println(json.toJSONString());
                }else {
                    String projectTeamID = request.getParameter("projectTeamID");
                    StudentProjectTeamBean sptb = new StudentProjectTeamBean();
                    sptb.setStudent_id(student.getStudent_id());
                    sptb.setProjectTeam_id(projectTeamID);
                    boolean msg = spts.add(sptb);
                    if(msg==true){
                        ProjectTeamService pts = new ProjectTeamService();
                        ProjectTeamBean ptb = pts.getById(projectTeamID);
                        ptb.setProjectTeam_count(String.valueOf(Integer.parseInt(ptb.getProjectTeam_count())+1));
                        System.out.println(ptb.getProjectTeam_count());
                        pts.change(ptb);
                    }
                    json.put("msg",msg);
                    System.out.println(json.toJSONString());
                }
            }
            response.getWriter().print(json.toJSONString());
        }
    }
}
